﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EpubFactory.Template.Domain
{
    public class MediaType
    {
        public string Name { get; set; }
        public string DefaultExtension { get; set; }
        public IEnumerable<string> Extensions { get; set; }
        public MediaType(string name, string defaultExtension)
            : this(name, defaultExtension, new string[] { defaultExtension })
        {

        }
        public MediaType(string name, string defaultExtension, IEnumerable<string> extensions)
        {
            this.Name = name;
            this.DefaultExtension = defaultExtension;
            this.Extensions = extensions;
        }
        public override bool Equals(Object other)
        {
            if (other is MediaType)
            {
                return this.Name.Equals(((MediaType)other).Name);
            }
            return false;
        }
        public override int GetHashCode()
        {
            if (this == null)
            {
                return 0;
            }
            return this.Name.GetHashCode();
        }
        public static bool IsBitmapImage(MediaType mediaType)
        {
            return mediaType.Equals(JPG) || mediaType.Equals(PNG) || mediaType.Equals(GIF);
        }
        public static MediaType DetermineMediaType(String filename)
        {
            return mediatypes.FirstOrDefault(g => g.Extensions.Any(d => d.Equals(filename, StringComparison.InvariantCultureIgnoreCase)));
        }
        //static MediaType()
        //{

        //}
        //public static Dictionary<String, MediaType> MediaTypesByName = new Dictionary<String, MediaType>();
        private static MediaType[] mediatypes = new MediaType[] {
        XHTML, EPUB, JPG, PNG, GIF, CSS, SVG, TTF, NCX, XPGT, OPENTYPE, WOFF, SMIL, PLS, JAVASCRIPT, MP3, MP4, OGG};
        public static readonly MediaType XHTML = new MediaType("application/xhtml+xml", ".xhtml", new String[] { ".htm", ".html", ".xhtml" });
        public static readonly MediaType EPUB = new MediaType("application/epub+zip", ".epub");
        public static readonly MediaType NCX = new MediaType("application/x-dtbncx+xml", ".ncx");

        public static readonly MediaType JAVASCRIPT = new MediaType("text/javascript", ".js");
        public static readonly MediaType CSS = new MediaType("text/css", ".css");

        // images
        public static readonly MediaType JPG = new MediaType("image/jpeg", ".jpg", new String[] { ".jpg", ".jpeg" });
        public static readonly MediaType PNG = new MediaType("image/png", ".png");
        public static readonly MediaType GIF = new MediaType("image/gif", ".gif");

        public static readonly MediaType SVG = new MediaType("image/svg+xml", ".svg");

        // fonts
        public static readonly MediaType TTF = new MediaType("application/x-truetype-font", ".ttf");
        public static readonly MediaType OPENTYPE = new MediaType("application/vnd.ms-opentype", ".otf");
        public static readonly MediaType WOFF = new MediaType("application/font-woff", ".woff");

        // audio
        public static readonly MediaType MP3 = new MediaType("audio/mpeg", ".mp3");
        public static readonly MediaType OGG = new MediaType("audio/ogg", ".ogg");

        // video
        public static readonly MediaType MP4 = new MediaType("video/mp4", ".mp4");

        public static readonly MediaType SMIL = new MediaType("application/smil+xml", ".smil");
        public static readonly MediaType XPGT = new MediaType("application/adobe-page-template+xml", ".xpgt");
        public static readonly MediaType PLS = new MediaType("application/pls+xml", ".pls");
    }
}
